<%@page import="java.util.Calendar"%>
<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<% response.setContentType("text/html; charset=UTF-8");%>
<% request.setCharacterEncoding("utf-8");%>
<%@ page import="java.sql.*" %>
<%@ page import="javax.xml.parsers.*" %>
<%@ page import="org.w3c.dom.*" %>
<%@ page import="java.io.File" %>
<%@ page import="java.util.Calendar" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%
	Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(getServletContext().getRealPath("/") + "web.xml"));
	doc.getDocumentElement().normalize();
	String dbhost = doc.getElementsByTagName("host").item(0).getTextContent();
	String dbport = doc.getElementsByTagName("port").item(0).getTextContent();
	String dbuser = doc.getElementsByTagName("user").item(0).getTextContent();
	String dbpass = doc.getElementsByTagName("pass").item(0).getTextContent();
	String dbname = doc.getElementsByTagName("database").item(0).getTextContent();
	String dbconnString = "jdbc:mysql://" + dbhost + ":" + dbport + "/" + dbname + "?setUnicode=true&characterEncoding=UTF-8";
	Class.forName("com.mysql.jdbc.Driver").newInstance();
	Connection dbconn = DriverManager.getConnection(dbconnString, dbuser, dbpass);
	Statement query = dbconn.createStatement();
%>

<%
	String type = null;
	int maTS = 0, id = 0;
	String hoten = null, username = null;
	HttpSession ss = request.getSession();
	if (ss.getAttribute("isLogin") == null) {
		response.sendRedirect("login.jsp");
	} else if (ss.getAttribute("isLogin").equals(false)) {
		response.sendRedirect("login.jsp");
	} else {
		type = ss.getAttribute("type").toString();
		if (type.equals("TS")) {
			maTS = Integer.parseInt(ss.getAttribute("maTS").toString());
			hoten = ss.getAttribute("hoten").toString();
		} else {
			id = Integer.parseInt(ss.getAttribute("id").toString());
			username = ss.getAttribute("username").toString();
		}
	}
%>

<%
	String maDT = request.getParameter("maDT");
	if (maDT == null) {
		return;
	}
	if (type.equals("QT")) {
		return;
	}
%>

<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>Thi trắc nghiệm online</title>
		<link rel="stylesheet" href="css/style.css" type="text/css" />
		<script type="text/javascript" src="js/general_functions.js"></script>
		<link rel="stylesheet" href="css/thi_lambai.css" type="text/css" />
		<script type="text/javascript" src="js/thi_lambai.js"></script>
	</head>
	<body>

		<!-- BEGIN Header -->
		<div id="header">
			<div id="header-content">
				<h1><span>styleshout</span></h1>
				<h2 id="slogan">Thi trắc nghiệm online</h2>
				<ul id="header-links">
					<li>
						<%
							if (type.equals("TS")) {
								out.print(hoten);
							} else {
								out.print(username);
							}
						%>
					</li>
					<li><a href="logout.jsp">Đăng xuất</a></li>
				</ul>
				<!-- Menu Tabs -->
				<ul>
					<li><a href="index.jsp" title="Trang chủ"><span>Trang chủ</span></a></li>
					<li><a href="thi.jsp" title="Thi trắc nghiệm"><span>Thi trắc nghiệm</span></a></li>
					<li><a href="thongke.jsp" title="Tổng hợp kết quả thi"><span>Tổng hợp kết quả thi</span></a></li>
					<li><a href="cauhoi.jsp" title="Soạn câu hỏi"><span>Soạn câu hỏi</span></a></li>
				</ul>
			</div>
		</div>
		<!-- END Header -->


		<!-- BEGIN Content -->
		<div id="content">
			<h1 style="text-align:center">LÀM BÀI THI</h1>

			<div class="baithi_info">
				<%
					ResultSet rs = query.executeQuery("select * from dethi left join loaidethi on loaidethi = maLDT where maDT = " + maDT);
					out.println("<table>");
					int thoigianlambai = 0;
					if (rs.next()) {
						out.println("<tr><th>Thí sinh</th><td>" + hoten + "</td></tr>");
						out.println("<tr><th>Mã đề thi </th><td>" + maDT + "</td></tr>");
						out.println("<tr><th>Loại đề thi </th><td>" + rs.getString("tenloai") + "</td></tr>");
						out.println("<tr><th>Thời gian làm bài </th><td><b>" + Integer.toString(rs.getInt("thoigian")) + " phút" + "</b></td></tr>");
						thoigianlambai = rs.getInt("thoigian");
					} else {
						return;
					}
					rs.close();
					out.println("</table>");

					// thêm vào table baithi
					Calendar calendar = Calendar.getInstance();
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					String sysDate = sdf.format(calendar.getTime());
					try {
						query.executeUpdate("insert into baithi (dethi, thisinh, thoigianbd, thoigiankt, diem) values ( "
								+ maDT + " , " + Integer.toString(maTS) + " , '" + sysDate + "' , '" + sysDate + "' , -1 )");
					} catch (Exception e) {
						out.println("<h1>Lỗi thêm bài thi : " + e.getMessage() + "</h1>");
						return;
					}
				%>
			</div>

			<div class="countdown">Thời gian còn lại : <span id="javascript_countdown_time"></span></div>

			<form action="thi_ketqua.jsp" method="get" name="form_baithi">

				<div class="baithi_noidung">
					<%
						rs = query.executeQuery("select * from ct_dethi join cauhoi on cauhoi = maCH where dethi = " + maDT + " order by thutu asc");
						int i = 1;
						while (rs.next()) {
							out.println("<div class='cauhoi'>");
							out.println("<div class='noidung'><b>Câu hỏi " + Integer.toString(i) + " : </b>" + rs.getString("noidung") + "</div>");
							// lựa chọn 1
							out.println("<div class='luachon'>");
							out.println("<input type='radio' name='" + Integer.toString(rs.getInt("cauhoi")) + "' id='" + Integer.toString(i) + "_1' value='1' />");
							out.println("<label for='" + Integer.toString(i) + "_1'>" + rs.getString("luachon1") + "</label>");
							out.println("</div>");
							// lựa chọn 2
							out.println("<div class='luachon'>");
							out.println("<input type='radio' name='" + Integer.toString(rs.getInt("cauhoi")) + "' id='" + Integer.toString(i) + "_2' value='2' />");
							out.println("<label for='" + Integer.toString(i) + "_2'>" + rs.getString("luachon2") + "</label>");
							out.println("</div>");
							// lựa chọn 3
							out.println("<div class='luachon'>");
							out.println("<input type='radio' name='" + Integer.toString(rs.getInt("cauhoi")) + "' id='" + Integer.toString(i) + "_3' value='3' />");
							out.println("<label for='" + Integer.toString(i) + "_3'>" + rs.getString("luachon3") + "</label>");
							out.println("</div>");
							// lựa chọn 4
							out.println("<div class='luachon'>");
							out.println("<input type='radio' name='" + Integer.toString(rs.getInt("cauhoi")) + "' id='" + Integer.toString(i) + "_4' value='4' />");
							out.println("<label for='" + Integer.toString(i) + "_4'>" + rs.getString("luachon4") + "</label>");
							out.println("</div>");
							//
							out.println("</div>");
							i++;
						}
						rs.close();

						try {
							rs = query.executeQuery("select * from baithi where dethi = " + maDT
									+ " and thisinh = " + Integer.toString(maTS) + " and thoigianbd = '" + sysDate + "'");
							rs.next();
							out.println("<input type='hidden' name='mabt' value='" + Integer.toString(rs.getInt("maBT")) + "' >");
							rs.close();
						} catch (Exception e) {
							out.println("<h1>Lỗi : " + e.getMessage() + "</h1>");
							return;
						}
					%>
				</div>

				<div class="form_submit">
					<input type="button" class="mybutton" value="Nộp bài" onclick=" if(confirm('Bạn muốn nộp bài?')) lambai_submit(); ">
				</div>

			</form>

		</div>
		<!-- END Content -->

		<script type='text/javascript' src='js/countdown.js'></script>
		<script type='text/javascript'>
			timeleft_function = function() {
				lambai_submit();
			};
			showtime_function = function(time) {
				window.status = time;
			};
			javascript_countdown.init( <% out.print(thoigianlambai * 60);%> , 'javascript_countdown_time');
		</script>

		<!-- BEGIN footer -->
		<div id="footer">
			<div id="footer-content">
				<div class="footer-left">
					<p style="text-align: right">
						&copy; copyright 2006-2011 <strong class="separator">www.styleshout.com</strong>
					</p>
				</div>
			</div>
		</div>
		<!-- END footer -->

	</body>
</html>
